package me.sealer.ssm.controller;

import me.sealer.ssm.constant.Dict;
import me.sealer.ssm.constant.ErrorCodeConstant;
import me.sealer.ssm.exception.SsmException;
import me.sealer.ssm.service.SmsService;
import me.sealer.ssm.token.Token;
import me.sealer.ssm.token.TokenManager;
import me.sealer.ssm.utils.MessageConverterUtil;
import me.sealer.ssm.utils.TokenImageBytesGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述： $ TokenController
 *
 * @author sealer
 * @email 1178884049@qq.com
 * @date 2019年12月02日 11时36分20秒
 */
@Controller
@RequestMapping("/token")
public class TokenController {
    @Resource(name = "imageTokenManager")
    private TokenManager imageTokenManager;

    @Resource(name = "resubmitTokenManager")
    private TokenManager resubmitTokenManager;

    @Resource(name = "phoneTokenManager")
    private TokenManager phoneTokenManager;

    @Resource(name = "webChineseSmsService")
    private SmsService webChineseSmsService;

    @RequestMapping("/getImageToken")
    @ResponseBody
    public byte[] createTokenImage(HttpServletRequest request) {
        Token token = imageTokenManager.createToken(request);
        byte[] tokenPicBytes = TokenImageBytesGenerator.createPic(token.getUniqueId());

        return tokenPicBytes;
    }

    @RequestMapping("/getResubmitToken")
    @ResponseBody
    public Map createResubmitToken(HttpServletRequest request) {
        Token token = resubmitTokenManager.createToken(request);

        Map resultMap = new HashMap(4);
        resultMap.put("resubmitToken", token.getUniqueId());

        return resultMap;
    }

    /**
     * 证码请做好以下几点防范：
     * 发送验证码1分钟只能点击发送1次；
     * 相同IP手机号码1天最多提交20次；
     * 验证码短信单个手机号码30分钟最多提交10次；
     * 在提交页面加入图形校验码，防止机器人恶意发送；
     * 在发送验证码接口程序中，判断图形校验码输入是否正确。
     *
     * @param request
     * @return
     */
    @RequestMapping("/getPhoneToken")
    @ResponseBody
    public Map createPhoneToken(HttpServletRequest request) {
        String phoneNumber = request.getParameter(Dict.PHONE_NUMBER);
        if (phoneNumber == null || "".equals(phoneNumber.trim())) {
            throw new SsmException(ErrorCodeConstant.TOKEN_PHONE_NUMBER_IS_NULL);
        }
        Token token = phoneTokenManager.createToken(request);
        String tokenUniqueId = token.getUniqueId();

        Map resultMap = new HashMap(4);

        // TODO 从数据库获取短信模板, 组装短信
//        String text = "验证码:8888";
        String text = "sealer ssm平台提示您： 您登录的验证码为: " + tokenUniqueId + ", 有效期为3分钟。";

        // TODO 判断返回值是否成功
        int retCode = webChineseSmsService.sendMessage(phoneNumber, text);

        resultMap.put(Dict.RETURN_CODE, Dict.SYSTEM_RETURN_CODE_SUCCESS);
        String message = MessageConverterUtil.getMessage(request, ErrorCodeConstant.SMS_SEND_SUCCESS);
        resultMap.put(Dict.RETURN_MESSAGE, message);
        return resultMap;
    }
}
